package com.example.fashion.android.util;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * Created by fashion on 2017/7/10.
 * 布局的工具类
 */

public class LayoutUtil {
    //点击功能的回调事件
    private ClickCallBack clickCallBack;

    public ClickCallBack getClickCallBack() {
        return clickCallBack;
    }

    public void setClickCallBack(ClickCallBack clickCallBack) {
        this.clickCallBack = clickCallBack;
    }

    /**
     * 创建一个线性布局
     * @param context
     * @param orientation
     * @param gravity
     * @return
     */
    public static LinearLayout getLinearLayout(Context context,int orientation,int gravity ){
        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(orientation);
        layout.setGravity(gravity);
        return layout;
    }

    /**
     * 添加功能列表
     * @param context
     * @param layout
     * @param resourceId
     * @param clickCallBack
     */
    public static void addFunctionList(Context context, LinearLayout layout, int resourceId, final ClickCallBack clickCallBack) {
        String[] resourceArray = getResourceData(context, resourceId);
        //添加功能列表
        for (int i = 0; i < resourceArray.length; i++) {
            Button button = new Button(context);
            button.setText(resourceArray[i]);
            button.setAllCaps(false);
            button.setGravity(Gravity.CENTER);
            button.setBackgroundResource(android.R.color.holo_blue_light);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.topMargin = 30;
            layoutParams.leftMargin = 30;
            layoutParams.rightMargin = 30;
            button.setLayoutParams(layoutParams);
            button.setTag(i);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    clickCallBack.onClickListener(Integer.parseInt(view.getTag().toString()));
                }
            });
            layout.addView(button);
        }
    }

    /**
     * 添加功能列表
     * @param context
     * @param layout
     * @param resourceArray
     * @param clickCallBack
     */
    public static void addFunctionList(Context context, LinearLayout layout, String[] resourceArray, final ClickCallBack clickCallBack) {
        //添加功能列表
        for (int i = 0; i < resourceArray.length; i++) {
            Button button = new Button(context);
            button.setText(resourceArray[i]);
            button.setAllCaps(false);
            button.setGravity(Gravity.CENTER);
            button.setBackgroundResource(android.R.color.holo_blue_light);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.topMargin = 30;
            layoutParams.leftMargin = 30;
            layoutParams.rightMargin = 30;
            button.setLayoutParams(layoutParams);
            button.setTag(i);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    clickCallBack.onClickListener(Integer.parseInt(view.getTag().toString()));
                }
            });
            layout.addView(button);
        }
    }

    /**
     * 根据资源id获得资源数据列表
     *
     * @param context
     * @return
     */
    private static String[] getResourceData(Context context, int resourceId) {
        return context.getResources().getStringArray(resourceId);
    }

    /**
     * 点击的回调方法
     */
    public interface ClickCallBack {
        void onClickListener(int tag);
    }
}




